home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 16 / cnews016.doc
Text File  |  1989-07-26  |  60KB  |  1,563 lines

  1.  
  2.       Issue 16                     C News                             1
  3.  
  4.  
  5.       *--------------------------------------------------------------*
  6.       |    C NEWS - International C Electronic Newsletter/Journal    |
  7.       |           "Dedicated to the Art of C Programming"            |
  8.       |                                                              |
  9.       |                      Founded 12/27/87                        |
  10.       *--------------------------------------------------------------*
  11.  
  12.  
  13.                               Table of Contents
  14.  
  15.       THE HEAP: Messages from the Editor   ......................    2
  16.  
  17.       BOOK REVIEW: by Arnie Cherdak    ..........................    3
  18.  
  19.       COPYRIGHT-IT: Product Review by Jim Singleton   ...........    5
  20.  
  21.       BEGINNERS CORNER: By Wayne Dernoncourt    .................    8
  22.  
  23.       SETS IN C (V1.0) by Arnold Cherdak  .......................   12
  24.  
  25.       ARTICLE SUBMISSION STANDARDS      .........................   25
  26.  
  27.       HOW TO GET HOLD OF US HERE AT CNEWS........................   26
  28.  
  29.  
  30.  
  31.       "C  News"  is  an  Electronic  Journal published by the C BBS in
  32.       Burke,  VA  on  a monthly basis. The subject for C News is the C
  33.       programming language, as well as any derivatives like C++.  
  34.  
  35.       All  readers  are  encouraged  to  submit  articles, reviews, or
  36.       comments  for submission.  C News is freely distributed, but can
  37.       not  be  sold  for a profit, or cannot have a charge assessed to
  38.       cover  distribution costs.  All articles, and reviews become the
  39.       property   of   C   News   and   cannot  be  included  in  other
  40.       publications   without   written  permission  from  the  C  News
  41.       Editorial  Staff.  To do so is in direct violation of the C News
  42.       License  agreement.   Copies  of  which are available from the C
  43.       BBS.    This  publication  is  Copyrighted  under  U.S. Copyright
  44.       Law.
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.       
  60.  
  61.  
  62.  
  63.  
  64.       Issue 16                     C News                             2
  65.  
  66.  
  67.       ================================================================= 
  68.       THE HEAP: Messages from the Editor   
  69.       ================================================================= 
  70.  
  71.  
  72.  
  73.       THANKS!!!         
  74.  
  75.            A couple  of weeks ago we published Issue 15 of C News after
  76.       a  long  vacation.   A  few  days later at the monthly C BBS user
  77.       meeting  we discussed the future of C News.  With the addition of
  78.       Dan  Kozak  and  Jim  Singleton to the staff here, we felt that C
  79.       News  could  continue  and would grow.  Dan has done an excellent
  80.       job  of  putting  some macros together for PROFF and has put this
  81.       issue  together.   Jim  Singleton reviewed the issue and provided
  82.       one  of  the  articles. With this level of support I look forward
  83.       to the rest of this second year of publication.   
  84.  
  85.       BARRY'S PET PROJECT       
  86.  
  87.            Well the  mailman  has been dropping off more postcards from
  88.       around  the  globe  these  last  couple of weeks.  Postcards have
  89.       found   there  way  here  from  Finland,  Abu  Dubai,  Iowa,  and
  90.       California.    Keep  up  the  good  work  and  keep  those  cards
  91.       coming!!  
  92.  
  93.       COMPUTER AIDED SOFTWARE ENGINEERING      
  94.  
  95.            Once again  the subject of CASE has crossed my desk.  I just
  96.       finished   a  class  in  Systems  Analysis  and  Design  and  was
  97.       impressed  with  some  of  the  PC  Based  CASE  tools  that  are
  98.       available.   When you look at the price tag though of some of the
  99.       packages  ($1500 in some cases) you realize that the only way you
  100.       will  get  a  chance to use one is to get the boss to by it! Yeah
  101.       right you say..  
  102.  
  103.            Over the  next  year  or  so  I  am going to be working on a
  104.       small  private project that is a combination of a SCCS system and
  105.       a  CASE  tool.   As  my  thoughts  on  the  mythical  project are
  106.       finalized  I will publish a series of articles here in C News.  I
  107.       would  like  to generate some conversation this subject here in C
  108.       News  and  on  the  C BBS.  If you have some thoughts on CASE and
  109.       would  like  some  editorial space, send me a note in Snail Mail,
  110.       MCI  Mail,  Fido Mail or call the C BBS at 703-644-6478.  I would
  111.       be  very  interested  in  hearing  how some of you are making out
  112.       with the various CASE tools that are currently available.   
  113.  
  114.            Look forward  to hearing from you, and now let's get on with
  115.       the 16th issue of C News.  
  116.  
  117.       Barry 
  118.  
  119.  
  120.  
  121.       
  122.  
  123.  
  124.       Issue 16                     C News                             3
  125.  
  126.  
  127.       ================================================================= 
  128.       BOOK REVIEW: by Arnie Cherdak    
  129.       ================================================================= 
  130.  
  131.  
  132.  
  133.                  The Waite Group's Essential Guide to ANSI C
  134.                               by Naba Barkakati
  135.  
  136.               Pub: Howard W. Sams & Co., Indianapolis, IN, 1988
  137.  
  138.                                     $6.95
  139.  
  140.            I've spent  a lot of money on books about computer languages
  141.       and  programming  but  this  inexpensive little gem is just about
  142.       the  best  bargain  I've come across yet.  The author claims that
  143.       it's  not  for  the  neophyte.   In  the author's words, it's for
  144.       "intermediate   to   professional   level  programmers."  It's  a
  145.       reference  guide that summarizes the basic features of the ANSI C
  146.       standard   (1988)   including   the  preprocessor,  new  language
  147.       features  as compared with the original K&R default standard, and
  148.       standard   library   functions.    It  also  provides  individual
  149.       reference  entries  for  each standard library routine along with
  150.       examples  of  their use.  All this and two quick reference guides
  151.       for  the  library routines as well as a fairly sizable index make
  152.       this book easy to use.  
  153.  
  154.            This book  is not organized for learning C.  That's why it's
  155.       not  supposed  to  be for the beginner if this is the only source
  156.       he  or she would have to use.  However, in conjunction with other
  157.       sources  of  instruction and a good ANSI C compiler, say, Turbo C
  158.       version  2.0 (which is almost an ANSI C compiler), this will make
  159.       a  dynamite reference.  The Turbo C 2.0 manuals have most of this
  160.       information  but  the  reader  needs  to wade through two volumes
  161.       full  of  all  kinds of stuff to get at the nuggets needed to use
  162.       the  language's  basic  features.   This reference guide has only
  163.       the  nuggets  and  a  few  clearly  written descriptions of basic
  164.       stuff  such  as  one  of  the  most  succinct  and understandable
  165.       descriptions  of  memory  allocation  procedures  I've  ever  run
  166.       across.   It has excellent descriptions of other subjects as well
  167.       in  the  introductions  to  the  other  9 chapters describing the
  168.       various groups of library functions.  
  169.  
  170.            After several   months  of  using  this  book,  I  find  the
  171.       reference  guides  for library functions to be invaluable.  There
  172.       are  other  books  like  this  but  none  I've  seen have similar
  173.       indexes  to  the  library  functions  and,  hence, are of dubious
  174.       value by comparison.  
  175.  
  176.            Also, in   using  the  function  descriptions  and  function
  177.       parameter  descriptions  with  Turbo  C  version  2.0, I've found
  178.       complete  agreement,  so  far,  which  means I can use this guide
  179.  
  180.  
  181.       
  182.  
  183.  
  184.       Issue 16                     C News                             4
  185.  
  186.  
  187.       which  is  much simpler in most respects than the Turbo Reference
  188.       Guide.  
  189.  
  190.            The Waite  Group  also  offers similar guides to Microsoft C
  191.       (ver  5.1) and Turbo C (ver 1.0, 1.5, and 2.0 in one volume) that
  192.       cover  similar  ground  including  the extensions offered by each
  193.       compiler.   These  are only a dollar more.  I opted for the plain
  194.       vanilla  ANSI standard book so that I could easily determine what
  195.       features  are  in  the  standard  and  not part of the compiler's
  196.       enhancements.  It makes writing portable code that much easier.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.       
  242.  
  243.  
  244.       Issue 16                     C News                             5
  245.  
  246.  
  247.       ================================================================= 
  248.       COPYRIGHT-IT: Product Review by Jim Singleton   
  249.       ================================================================= 
  250.  
  251.  
  252.       Product:       COPYRIGHT-IT, version 1.0809
  253.       Cost:          $25.00
  254.       Manufacturer:  Synthetic Intelligence Inc.
  255.                      286 Fifth Avenue, Suite 707
  256.                      New York, NY  10001
  257.  
  258.            If you  ask a half dozen programmers, of varying experience,
  259.       how  to  copyright  a  program,  the chances are pretty good that
  260.       you'll  get  a  half dozen different answers.  One way which just
  261.       about  everyone agrees is correct is to file for a copyright with
  262.       the  United  States Copyright Office.  Unfortunately, many people
  263.       think  that  this  is difficult.  Unlike a patent, which requires
  264.       quite  a  bit  of  work  (and  usually  an  attorney), filing the
  265.       paperwork  for  a  copyright is not too difficult.  COPYRIGHT-IT,
  266.       from   Synthetic   Intelligence  Inc.,  makes  the  process  even
  267.       easier.   
  268.  
  269.            To file  for  a  copyright, one must fill out Form TX, which
  270.       can   be  obtained  from  the  United  States  Copyright  Office.
  271.       COPYRIGHT-IT  guides  an  author  through  the completion of Form
  272.       TX.   (It should be pointed out that Form TX and COPYRIGHT-IT are
  273.       not  just  for  software,  but  for  any  material  which  can be
  274.       copyrighted.)   Two blank copies of Form TX are included with the
  275.       package  and  additional  copies  of  Form  TX may be obtained by
  276.       calling the Copyright Office (202-287-9100).  
  277.  
  278.            Opening the  package  reveals  a  sheet of instructions, two
  279.       blank  copies  of  Form  TX, a sample of a completed Form TX, and
  280.       the  program  disk.  (The program has no color or graphics, so it
  281.       should  run  on any monitor/video card combination.) The one page
  282.       instructions  are  clear  and concise, and these instructions are
  283.       also  included  on the disk.  One might be tempted to just glance
  284.       at  the  sample  copy  of  Form  TX and just get started with the
  285.       program,   but   it   does   contain   a  very  useful  piece  of
  286.       information.   The  sample  copy shows how to correctly align the
  287.       form  for  printing.  This is covered in the instructions, but it
  288.       is  helpful  to see where the alignment dots should appear on the
  289.       form.   (While  the  alignment dots are printed on the form, they
  290.       do not deface the form and are hardly noticeable.) 
  291.  
  292.            COPYRIGHT-IT starts  out  by displaying the copyright notice
  293.       and  the warranty information.  These are pretty standard and the
  294.       disk is warranted free from defects, etc. for 90 days.  
  295.  
  296.            The next  two screens describe the program and the copyright
  297.       process.   COPYRIGHT-IT  is  described as a tutorial program.  In
  298.       addition  to  guiding the user through the process of filling out
  299.  
  300.  
  301.       
  302.  
  303.  
  304.       Issue 16                     C News                             6
  305.  
  306.  
  307.       copyright  applications,  the  user  can  learn quite a bit about
  308.       copyrights  by  using  the  full  program.  The program describes
  309.       what  a  copyright  is, what can be copyrighted, why works should
  310.       be  copyrighted, and other information.  In addition, the program
  311.       discusses  what  additional  protection,  other  than  a standard
  312.       copyright, is available for software.  
  313.  
  314.            After all  this  information and a list of a few last minute
  315.       instructions,  the program's main menu appears, offering the user
  316.       a  choice  of  completing  Form  TX  or Form PA.  (Form PA is for
  317.       copyrighting  screen  output  and  requires an additional module,
  318.       which  is available for $19.25.)  The next screen is a listing of
  319.       all  the  entries  on  side  one  of  Form  TX.  A set of default
  320.       information  is  already  entered  and can be written over.  This
  321.       information    is,    according    to   Synthetic   Intelligence,
  322.       representative  of  80%  of  the copyright applications which are
  323.       filed.   There  is  no reason to doubt this claim, as the default
  324.       information  basically  consists  of  the  title,  author's name,
  325.       addresses,  etc.   After  completing  the first half of the form,
  326.       the  program  asks  if  the  information  is  correct  and if the
  327.       printer  is  ready.   If the paper is not properly aligned in the
  328.       printer,  the  user  is allowed to set the paper before printing.
  329.       This  is  probably the biggest drawback to the program, because a
  330.       user might want to fill out an entire form before printing.  
  331.  
  332.            After printing  the first page, COPYRIGHT-IT guides the user
  333.       through  the  completion  and printing of the second page.  After
  334.       the  second  page  is  completed,  the  program displays one last
  335.       screen  of  information,  consisting  of what steps the user must
  336.       follow  to  send  the  form  (i.e.,  the  $10  filing fee and the
  337.       address   and   phone   number   of  the  Copyright  Office)  and
  338.       instructions  on  how  to  obtain, if necessary, special handling
  339.       (for $200).  
  340.  
  341.            COPYRIGHT-IT has  a  comprehensive  help  system,  with help
  342.       available  for  each  data field.  To obtain help on a particular
  343.       item,  all  the  user has to do is press "F1" and a definition of
  344.       that  particular  field appears.  If additional help is required,
  345.       the  user can display an interpretation of that data field and an
  346.       example.  
  347.  
  348.            All in  all,  COPYRIGHT-IT  is  a  good program.  It greatly
  349.       simplifies  the  completion of Form TX and contains a quite a bit
  350.       of  useful  information.   As  good as COPYRIGHT-IT is, it is not
  351.       without  its  drawbacks.   At times it runs a bit slow, which can
  352.       be   especially  annoying  on  a  slower  machine.   The  biggest
  353.       drawback,  however,  is  that  COPYRIGHT-IT  is designed to print
  354.       each  side  of  the  form  as soon as it has been filled out.  It
  355.       would  be  preferable  if the entire form was completed first and
  356.       then  the  user was given an option to save the data or print the
  357.       form.   When  the  data  entry section of the program appears, it
  358.       contains  the  information  entered the last time the program was
  359.  
  360.  
  361.       
  362.  
  363.  
  364.       Issue 16                     C News                             7
  365.  
  366.  
  367.       run.   While  this  is  not  a  problem,  there is no way to save
  368.       information  each  time  the  data is entered, so a separate data
  369.       file  cannot  be  maintained for each application.  The main menu
  370.       should  also  be  expanded,  allowing  for these options, that is
  371.       allowing  the  user  to  retrieve  a  saved data file and to just
  372.       print a file.  
  373.  
  374.            In spite   of  these  minor  drawbacks,  COPYRIGHT-IT  is  a
  375.       valuable  program, well worth its cost.  With COPYRIGHT-IT, there
  376.       is  no  reason  for  any programmer not to copyright his software
  377.       just  because  he  thinks  filling  out  the appropriate forms is
  378.       difficult.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.       
  422.  
  423.  
  424.       Issue 16                     C News                             8
  425.  
  426.  
  427.       ================================================================= 
  428.       BEGINNERS CORNER: By Wayne Dernoncourt    
  429.       ================================================================= 
  430.  
  431.  
  432.  
  433.            It's time  for  the showers so that we can have May flowers.
  434.       Flowers  are  the  result (output, if you will) of water, sun and
  435.       seeds  (inputs, if you will).  With that as an introduction, I'll
  436.       continue  the  series  of  articles  on  C and doing input to a C
  437.       program.   This  month,  we'll  do  basic  data  entry  from  the
  438.       keyboard.   In  a  future  article,  we'll  discuss  reading  and
  439.       writing  to  files,  as  well  as  other  methods  of reading the
  440.       keyboard.  
  441.  
  442.            The subject  of  data  entry  is confusing to say the least.
  443.       This  isn't  to  say that is impossible, just that it's difficult
  444.       at times to understand.  The basic command required for this is: 
  445.  
  446.       scanf ("arg.list.1", arg.list.2); 
  447.  
  448.       As  you  can  see  the scanf function accepts two argument lists.
  449.       The  first  argument  list  looks  like  the  argument  list that
  450.       describes  variable  output  on  the printf function.  The second
  451.       argument  list lists the variables that are to be filled from the
  452.       first  argument list.  This is different from the printf function
  453.       since  the  second  argument  list  was  optional.   If it wasn't
  454.       given,  the  only  thing  printed  was  the  text  from the first
  455.       argument  list.   If  both  argument  lists aren't present in the
  456.       scanf  function,  then  the  compiler  will have a difficult time
  457.       either  interpreting  how  to read the data from the keyboard, or
  458.       where to store the data.  
  459.  
  460.            If you'll   remember  from  the  discussion  on  the  printf
  461.       function,  the  basic  format  of  the  first argument list was a
  462.       series  of variable formatting commands %%f, %%d, %%c, %%s, etc.,
  463.       interspersed  with  text  to print.  These are consistent and are
  464.       used with the scanf function.  
  465.  
  466.            Also from  the  discussion on the printf function, the basic
  467.       format  of  the  second  argument  list  was  a list of variables
  468.       separated  by  commas.   This  is  the  part that is a little bit
  469.       confusing  to some people.  On the scanf function, there is still
  470.       a  comma  separated  variable  list,  but  with  a  change,  each
  471.       variable  that  isn't  a character or string variable is preceded
  472.       with a &.  
  473.  
  474.            As a  refresher, I'm going to list the two source files that
  475.       I've  changed  (INIT.C & GET_DATA.C).  After the two source files
  476.       is  a  discussion as to why they were changed and their affect on
  477.       the  other modules.  The first module is the main module and goes
  478.       into a file called INIT.C.  
  479.  
  480.  
  481.       
  482.  
  483.  
  484.       Issue 16                     C News                             9
  485.  
  486.  
  487.  
  488.       #include <stdio.h>
  489.       float get_data (int samp_size);    /* This has changed */
  490.       float compute (int samp_size, float x_sum);
  491.       void print_results (float average);
  492.  
  493.       main ()
  494.  
  495.       /*   This program is to find the mean (average) and standard
  496.       deviation of a series of numbers.  The numbers will be read in
  497.       from the keyboard and printed on the screen.  Enter -1 when
  498.       finished                                                    */
  499.  
  500.            /* initialization of variables */
  501.       /* 1. Declare all variables needed and initialize them to zero.*/
  502.       {
  503.            int samp_size=0; /* samp_size: number of variables read in*/
  504.            float sum_of_x=0.0;   /* the sum of the variables read in*/
  505.            float answer;
  506.  
  507.            samp_size = 4;  /* Get four data points from the keyboard */
  508.                            /* The location of this statement changed */
  509.  
  510.            /* get the data needed by the program */
  511.            sum_of_x = get_data(samp_size);    /* This has changed */
  512.  
  513.            /* Now compute the answer */
  514.            answer = compute(samp_size, sum_of_x);
  515.  
  516.            /* Print the results out */
  517.            print_results (answer);
  518.  
  519.            /* Closing the brace on the main routine exits the
  520.            program.  There are other ways out, but this way will do for
  521.            now.*/
  522.       }
  523.  
  524.       The  following  goes into the file called GET_DATA.C.  Notice the
  525.       changes  that  have  been  made  (go back and compare the program
  526.       from the November column).  
  527.  
  528.       /*   2. Get samples from the keyboard.  The number of samples to
  529.            read will be controlled by a number passed to this function
  530.            by the calling program. */
  531.  
  532.            float get_data(int samp_size) /* Statement change */
  533.       {
  534.            float sum_x=0.0, sample;      /* Statement change */
  535.            integer i;
  536.  
  537.            for (i=1; i<samp_size; i++)   /* This group of statements */
  538.                 {                        /* is new. */
  539.  
  540.  
  541.       
  542.  
  543.  
  544.       Issue 16                     C News                            10
  545.  
  546.  
  547.                 printf("\nEnter sample point: %%d", i);
  548.                 scanf ("%%f", &sample);
  549.                 sum_x += sample;         /* New group ends here */
  550.                 }
  551.       /* Now is the time to return back to the main program. */
  552.            return(sum_x);
  553.       }
  554.  
  555.            There are  three  big  changes  which you may notice.  First
  556.       and  maybe  the  most  straight-forward  is  the  addition of the
  557.       integer  variable  samp_size  to  the  get_data  prototype in the
  558.       INIT.C  file  along  with  it's addition to the function call and
  559.       subsequent declaration.  
  560.  
  561.            The second  is the construct for (i=1; i<samp_size; i++) and
  562.       a  set  of  curly  braces ("{}") enclosing some C reserved words.
  563.       This  is  the  basic  loop  control  mechanism  in  C.  There are
  564.       variations  on  this,  and  they all have their uses, but this is
  565.       the  basic  one.  There are four sections to this construct.  The
  566.       first,  i=1,  initializes  the  variable  i  (it  still has to be
  567.       declared   as   being   accessible   to   the   function).   This
  568.       initialization  can  be  to any value, including a variable.  The
  569.       second  section  is  the test section, as long as this section is
  570.       true,  do the C statement following it.  The third section is the
  571.       action  to be performed after the loop section has executed.  The
  572.       fourth  section  is  the  whole  reason for the loop mechanism to
  573.       exist,  that  is  the  set  of actions the program is supposed to
  574.       perform,  the C commands inside of the curly braces.  If you will
  575.       remember  in  the November issue, I said that to C, the following
  576.       two sentences were equal: 
  577.  
  578.            {
  579.            In
  580.            English,
  581.            this
  582.            is
  583.            the
  584.            same.
  585.            }
  586.  
  587.       as 
  588.  
  589.            In English, this is the same.
  590.  
  591.            The curly  braces  group  a set of statements that are to be
  592.       executed  every  time the loop is executed.  Each of the sections
  593.       may  contain  any  valid  C  expressions.   As long as the second
  594.       section  evaluates  to  true  the  loop  will  be  executed.  The
  595.       expression  that  you may have noticed in the third section, i++,
  596.       but  couldn't  quite  decide  what  it  does  is  called the post
  597.       increment  operator.   If you examine a lot of programs, a lot of
  598.       the  time in looping is spent incrementing something by one.  The
  599.  
  600.  
  601.       
  602.  
  603.  
  604.       Issue 16                     C News                            11
  605.  
  606.  
  607.       people  that  designed  the language realized this and decided to
  608.       take  advantage  of this by using an instruction that is found on
  609.       a  lot  of  CPUs called the increment instruction.  The increment
  610.       instruction  is  faster for the CPU to execute and is also easier
  611.       for  the programmer to see what is going on.  It may look awkward
  612.       at  first, but after you get used to it, it is very powerful.  By
  613.       the  way,  there  is  also  a  decrement  operator.  I'm going to
  614.       reserve the issue of these operators until a later issue.  
  615.  
  616.            The third  thing  that you may notice, and the point of this
  617.       whole  discussion  is  the  scanf command.  Notice the similarity
  618.       between   scanf  and  printf.   The  biggest  difference  is  the
  619.       addition  of  the & in front of the variable to receive the value
  620.       read  in  from  the  keyboard  (remember  this  is  only used for
  621.       integer  and  real numbers, not for characters or strings).  In a
  622.       next  months column, I'll discuss what the & is and other ways it
  623.       can be used.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.       
  662.  
  663.  
  664.       Issue 16                     C News                            12
  665.  
  666.  
  667.       ================================================================= 
  668.       SETS IN C (V1.0) by Arnold Cherdak  
  669.       ================================================================= 
  670.  
  671.  
  672.  
  673.            Before I  "fell  in love" with C, I, like many others, wrote
  674.       a  great  deal  of software in Pascal.  Indeed, my recent pursuit
  675.       of  a Master's Degree in Computer Science has resulted in a large
  676.       number   of  projects,  etc.,  written  in  Pascal.   Despite  my
  677.       affection  for  C,  I  must  admit to it having some shortcomings
  678.       compared  with  Pascal.   In  particular,  Pascal's facility with
  679.       sets  is  sorely  missed by me in C.  I have seen some approaches
  680.       to  C  set  implementations and have tried to use them.  However,
  681.       they  either  were very incomplete or suffered from being part of
  682.       another  software package so I lost patience before I was able to
  683.       understand  them.   This  effort  is  an  attempt  to  provide  a
  684.       complete  capability  with  only  a  few  omissions  and,  I must
  685.       confess,  also  attempts to use some of the features I liked best
  686.       in other approaches.  
  687.  
  688.            In Pascal,  I  was/am  regularly  able to write a convenient
  689.       expression such as the following: 
  690.  
  691.                VAR X : CHAR;
  692.                .
  693.                     .
  694.                     .
  695.                IF X IN ['A'..'F','a'..'f'] THEN ...
  696.  
  697.       Or,  if  I  had  assigned  the  character ranges to a SET OF CHAR
  698.       variable, earlier, I could have said 
  699.  
  700.                VAR CHAR_VAR : SET OF CHAR;
  701.                           X : CHAR;
  702.                          .
  703.                          .
  704.                CHAR_VAR := ['A'..'F','a'..'f'];
  705.                          .
  706.                          .
  707.                          .
  708.                IF X IN CHAR_VAR THEN ...
  709.  
  710.            To provide  that facility in C along with all the other neat
  711.       things  that  can  be  done  with  Pascal's sets required several
  712.       steps, namely: 
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.       
  722.  
  723.  
  724.       Issue 16                     C News                            13
  725.  
  726.  
  727.  
  728.       1. DEFINE REQUIREMENTS FOR SET OPERATIONS AND FEATURES  
  729.  
  730.            This wasn't  too difficult since I started with several good
  731.       references including the following: 
  732.  
  733.  
  734.            [1] Cooper,  Doug,  STANDARD  PASCAL, USER REFERENCE MANUAL,
  735.                1983, W.W. Norton & Co., New York.  
  736.  
  737.            [2] Koffman,   E.   B.,   PROBLEM   SOLVING  AND  STRUCTURED
  738.                PROGRAMMING     IN    PASCAL,    Second    Ed.,    1985,
  739.                Addison-Wesley, Reading, Mass.  
  740.  
  741.            [3] The  Institute  of Electrical and Electronics Engineers,
  742.                Inc.,  AN  AMERICAN  NATIONAL  STANDARD,  IEEE  STANDARD
  743.                PASCAL    COMPUTER   PROGRAMMING   LANGUAGE   (ANSI/IEEE
  744.                770X3.97-1983), 1983, Wiley.  
  745.  
  746.  
  747.            A set  is  defined [1] as a group of values of any data type
  748.       that  is  enumerable.   That  is,  the  values of the type can be
  749.       numbered   and   can   be  compared  for  equality  and  relative
  750.       position.  They are, in other words, ordinal.   
  751.  
  752.            The particular  data  type  of  which  the  set  consists is
  753.       called  the  set's base type.  When performing certain operations
  754.       on  one  or  more sets, it is only logical that all sets involved
  755.       in  the  operation  have  the  same  base type.  For example, you
  756.       wouldn't  want  to  add  apples to oranges unless, of course, you
  757.       were  making  fruit  salad.  This  brings about a requirement for
  758.       type checking in set operations.  
  759.  
  760.            The C  language  has,  as  does  Pascal, a number of ordinal
  761.       data types as listed below: 
  762.  
  763.           boolean
  764.           integer
  765.           character
  766.           enumerated types
  767.           subranges
  768.  
  769.       Boolean  types  have only two values, TRUE and FALSE.  These are,
  770.       however, ordinal, and can form a legitimate set.   
  771.  
  772.            Integers are   an  infinite  sized  set  and  for  practical
  773.       purposes  are  limited by the maximum integer value representable
  774.       on  the  computer  in  use.   For  typical  personal computers, a
  775.       16-bit  integer  would  have  a  maximum  value  of  32K  (signed
  776.       integers).   This  is really a subrange of integers and I decided
  777.       to  remove  the  type  integer  and  allow  subrange to represent
  778.       integer types in the practical sense.   
  779.  
  780.  
  781.       
  782.  
  783.  
  784.       Issue 16                     C News                            14
  785.  
  786.  
  787.  
  788.            While negative  values can be ordinal and sets in Pascal can
  789.       be  of  negative  values,  this  complication was avoided for the
  790.       current  release  of  "sets."   I  have  not tested the code with
  791.       negative  set member values and I don't know what would happen if
  792.       they were used. I'll look into that in the future.  
  793.  
  794.            Character type  is  a common type of set base type, at least
  795.       in  my  own  software.   The  basic  implementation is capable of
  796.       handling a full 256 character set.  
  797.  
  798.            Finally, Enumerated  types  are  a primary feature of Pascal
  799.       and  sets  of  these  types  are  commonly  used.   ANSI  C makes
  800.       provision   for  Enumerated  types  but  without  type  checking.
  801.       Subranges of Enumerated types are, of course, allowed.  
  802.  
  803.            Given all  the  above  base  types,  the  next  step  was to
  804.       determine  what  operations were to be performed on sets of these
  805.       base  types.   For this I used references 1 and 3 and made a list
  806.       of  operations  on sets that were required by the industry Pascal
  807.       standard.   The  following  is  the list of operations I gathered
  808.       from these references: 
  809.  
  810.  
  811.            a) Define a set type and a NULL set 
  812.  
  813.            b) Construct & populate a set 
  814.  
  815.            c) Assign  a set value to a set variable or from one  set to
  816.               another and add or delete members of a set.  
  817.  
  818.            d) Perform   set   multiplication  (intersection),  addition
  819.               (union), and subtraction (difference) 
  820.  
  821.            e) Perform  relational  test operations namely a = b   set a
  822.               equals  set  b  a <> b  set a does not equal set b a <= b
  823.               set  a  is included in set b a >= b  set a includes set b
  824.               a IN b  ordinal value, a, is a member of set, b 
  825.  
  826.            f) In  addition, several operations not found in the  Pascal
  827.               standards   were   considered  desirable  including   the
  828.               ability to display set values in several ways.  
  829.  
  830.  
  831.  
  832.       2. PREPARE A SOFTWARE DESIGN     
  833.  
  834.            First, I  needed  to  define  the  data type, set.  Well, it
  835.       needed  to  have  a  base  type as discussed above.  It needed to
  836.       accommodate  as many as 32K members and, more typically, only 256
  837.       members  while  not  consuming  enormous  amounts of storage.  It
  838.       needed  to  be easily definable within a C function and it had to
  839.  
  840.  
  841.       
  842.  
  843.  
  844.       Issue 16                     C News                            15
  845.  
  846.  
  847.       disappear  automatically  when the function execution was done or
  848.       when  the  program  needed it to disappear.  When it disappeared,
  849.       it could not consume storage.  
  850.  
  851.            The C  structure  was  the logical result and is shown below
  852.       along with a definition of the base types allowable: 
  853.  
  854.       /* Base Types come from an enumerated type definition. */
  855.       typedef enum {UNIVERSAL, BOOLEAN, 
  856.               CHARACTER, ENUMERATED, SUBRANGE} base;
  857.  
  858.       /* The following structure IS a set */
  859.       typedef struct SET {
  860.         base base_type;   /* base ordinal type for the set */
  861.         int set_size;     /* max allowable set size        */
  862.         int nmembers;     /* number of members assigned    */
  863.         int member_recs;  /* number of member-holding words*/
  864.         int set_tag;      /* additional typing feature     */
  865.                                     /* member-holding words*/
  866.         unsigned word[MAX_MEMBERS / MEMBERS_PER_WORD];
  867.       } set;
  868.  
  869.       Each  instance  of  a  set  object is a set variable or set.  Set
  870.       creation  is  performed  by  defining a set object using normal C
  871.       defining statements such as 
  872.  
  873.  
  874.            struct SET set_name; 
  875.  
  876.       The base_type field was explained previously.   
  877.  
  878.            The set_size  field  indicates  the  maximum  number  of set
  879.       members  allowable under this particular set definition.  This is
  880.       not  to  be  confused with the amount of member storage permitted
  881.       in the array, "word."   
  882.  
  883.            The nmembers   field  denotes  the  number  of  set  members
  884.       currently  assigned  to  the  set.   For  example,  if  a  set of
  885.       CHARACTER  is  defined  to  have  a  set_size  of  256,  it could
  886.       conceivably  hold  as  many as 256 set_members.  If only 'A'..'Z'
  887.       were  assigned  to  the  set,  then  nmembers  would  contain the
  888.       number,  26.  It follows logically that a set without any members
  889.       -- i.e.- nmembers = 0 -- is an empty or a NULL set.  
  890.  
  891.            The array,  "word,"  contains  a  number  of words (unsigned
  892.       integers)   determined   by   the   constants   MAX_MEMBERS   and
  893.       MEMBERS_PER_WORD.   These  constants and their definitions may be
  894.       found  in  file,  SETS.H,  and are implementation-specific.  Feel
  895.       free  to  alter them but note that MEMBERS_PER_WORD is a physical
  896.       limitation for most Personal Computers.   
  897.  
  898.            Note that  all  sets will have an array, "word," of the size
  899.  
  900.  
  901.       
  902.  
  903.  
  904.       Issue 16                     C News                            16
  905.  
  906.  
  907.       determined  here.   However,  the  number  of "word" array values
  908.       used  for  a  set  may  be  less than this.  For a 16-bit integer
  909.       machine  and  MAX_MEMBERS  =  256,  the  word  array will have 16
  910.       values  (256/16).  If  the set_size is 20, only two values in the
  911.       word  array  are  required and the member_recs field will contain
  912.       the  value,  2.   The remainder of the word array will be present
  913.       but unused.  
  914.  
  915.            With the  default  values for the constants given in SETS.H,
  916.       each  set  definition will require 42 bytes on a typical Personal
  917.       Computer  using  Turbo-C.   If the value of MAX_MEMBERS is raised
  918.       to  32767  (max  value  for  a signed integer, i.e.- nmembers, in
  919.       Turbo-C,  Version 2.0), 2058 bytes would be required for a single
  920.       set.  
  921.  
  922.            Finally, the  set_tag  field  is  an  additional  means  for
  923.       defining  set  type.   For example, definition of a set of values
  924.       for  the  first  letter  of valid credit cards may be set_size=10
  925.       and  base_type=  CHARACTER.  It wouldn't do to be able to combine
  926.       that  set,  inadvertently, with a set of question responses which
  927.       might   also  be  of  set_size=10  and  base_type=CHARACTER.   To
  928.       prevent  that  type  of  an  error, assign a value to the set_tag
  929.       field  and  the required type checking will be carried out by the
  930.       sets  software.   In  the  examples  presented with this package,
  931.       set_tag is always made 0.  
  932.  
  933.            In defining  a  set, it is required to initialize certain of
  934.       the  structure fields including base_type, set_size, set_tag, and
  935.       to  give  the  set  a  name.   This could be left to the user who
  936.       would  have  to  know  about initializing structure field values.
  937.       However,  I  felt  that  this job should be made a bit simpler if
  938.       possible.   A  macro  called  "defset"  was written to create the
  939.       structure  and to perform the required initialization.  While the
  940.       user  still  has  to enter the initial values, it seems easier to
  941.       express  them as function arguments rather than as initialization
  942.       values.  Defset is shown below: 
  943.  
  944.       /* This macro provides a set declaration and should be used 
  945.          whenever a set object must be declared.
  946.       */
  947.       #define defset(set_name,bastype,size,tag) struct SET set_name\
  948.        = {bastyp,size,0,((size/MEMBERS_PER_WORD)\
  949.        +(size%MEMBERS_PER_WORD?1:0)),tag}
  950.  
  951.       To use defset to define a set in a function, merely enter 
  952.  
  953.  
  954.            defset(set_name,bastype,size,tag); 
  955.  
  956.       in  the  same  location  at  the  top of the function where other
  957.       variables  are defined and the C preprocessor does the rest.  Use
  958.       the  set  as  you would any other structure.  You may examine its
  959.  
  960.  
  961.       
  962.  
  963.  
  964.       Issue 16                     C News                            17
  965.  
  966.  
  967.       contents  or  you  may  use the functions in "sets" to manipulate
  968.       the  structure.   It  would  be  best  not  to  alter  any of the
  969.       structure   fields   by  any  means  except  through  the  "sets"
  970.       functions since all the field values are needed by "sets." 
  971.  
  972.            The functions  required  to  perform  the list of operations
  973.       presented  above are listed below as function prototypes together
  974.       with brief descriptions of each function: 
  975.  
  976.  
  977.            a) void dump_set(set *aset); 
  978.  
  979.                    This function  dumps  a  description of a set out to
  980.               stdout.   The  function  uses  the argument, pointer to a
  981.               set, to get to the set data.  
  982.  
  983.            b) void set_print(set *aset); 
  984.  
  985.                    Prints current  members  of  the  set.  The function
  986.               uses a pointer to a set to get to the set data.  
  987.  
  988.            c) unsigned wordlength(void); 
  989.  
  990.                    This determines  the  number  of  bits in a standard
  991.               unsigned  integer.   This is not a "sets" function.  It's
  992.               a  convenient  way  to determine the number of bits in an
  993.               integer, however.  
  994.  
  995.            d) int add_member(set *aset, int member); 
  996.  
  997.                    Sets a  bit  in the member'th bit place of the set's
  998.               member  record.   Takes  a  pointer  to  a  set and a set
  999.               member  ordinal  value  and  returns constants SUCCESS if
  1000.               the  set  accepted  the  new  member, NOCHANGE if the set
  1001.               already  had  that  member  present,  or  FAILURE  if the
  1002.               addition failed.  
  1003.  
  1004.            e) int del_member(set *aset, int member); 
  1005.  
  1006.                    Resets a  bit  in  the  member'th  bit  place of the
  1007.               set's  member record.  Takes a pointer to a set and a set
  1008.               member  ordinal  value  and  returns constants SUCCESS if
  1009.               the  member was deleted, NOCHANGE if the set  already had
  1010.               that member deleted, or FAILURE if the deletion failed.  
  1011.  
  1012.            f) boolean in_set(set *aset, int entity); 
  1013.  
  1014.                    Determines if  entity is a member of the set.  Takes
  1015.               a  pointer  to  a  set and a set member ordinal value and
  1016.               returns  constants  TRUE  if  the set contains the member
  1017.               specified  by  the  ordinal  value  and  FALSE if it does
  1018.               not.  
  1019.  
  1020.  
  1021.       
  1022.  
  1023.  
  1024.       Issue 16                     C News                            18
  1025.  
  1026.  
  1027.            g) int set_assign(set *destination, set *source); 
  1028.  
  1029.                    This function  assigns a set value to a set variable
  1030.               or  the  value  of  one set variable to another.  Takes a
  1031.               pointer  to  a  source set and a pointer to a destination
  1032.               set.   Returns  SUCCESS  if  the  assignment  is made and
  1033.               FAILURE if it is not.  
  1034.  
  1035.            h) void set_clear(set *aset); 
  1036.  
  1037.                    This function  clears all members in a set.  Takes a
  1038.               pointer  to  a set and deletes all set members to form an
  1039.               empty set.  
  1040.  
  1041.            i) int check_set_types(set *set1, set *set2); 
  1042.  
  1043.                    Type checking is done here in three ways: 
  1044.  
  1045.                    The base_types  of  destination  and  source must be
  1046.               the  same  and  both destination and source must have the
  1047.               same  set_size  and  set  tag.  UNLESS, of course, one of
  1048.               the  base_types is UNIVERSAL.  Takes pointers to two sets
  1049.               and  returns SUCCESS if for UNIVERSAL base_type, set_tags
  1050.               are  equal and the set containing the UNIVERSAL base_type
  1051.               has  a  larger  or equal set size than the other set.  If
  1052.               no  base_types are UNIVERSAL, SUCCESS is returned only if
  1053.               base_type,  set_tags,  and set_size are the same for both
  1054.               sets.  Otherwise, FAILURE is returned.  
  1055.  
  1056.            j) int set_intersect(set *intersect, set *set1, set *set2); 
  1057.  
  1058.                    This procedure     computes     the     intersection
  1059.               (set-product)  of  two sets.  The sets must have the same
  1060.               base_type   in   order  for  this  to  work.   The  input
  1061.               parameters  are  left  unchanged.   Takes pointers to two
  1062.               input  sets  (set1  and set2) and an output set that will
  1063.               contain  the intersection of the two input sets.  Returns
  1064.               SUCCESS  if  the intersection can be computed, otherwise,
  1065.               FAILURE is returned.  
  1066.  
  1067.            k) int set_union(set *xunion, set *set1, set *set2); 
  1068.  
  1069.                    This procedure  computes  the union (set-sum) of two
  1070.               sets.   The  sets  must  have the same base_type in order
  1071.               for   this  to  work.   The  input  parameters  are  left
  1072.               unchanged.   Takes  pointers  to two input sets (set1 and
  1073.               set2)  and  an  output set that will contain the union of
  1074.               the  two input sets.  Returns SUCCESS if the union can be
  1075.               computed, otherwise, FAILURE is returned.  
  1076.  
  1077.            l) int  set_difference(set  *difference,  set  *minuend, set
  1078.               *subtrahend); 
  1079.  
  1080.  
  1081.       
  1082.  
  1083.  
  1084.       Issue 16                     C News                            19
  1085.  
  1086.  
  1087.  
  1088.                    This procedure   computes   the  difference  of  two
  1089.               sets.   The  sets  must  have the same base_type in order
  1090.               for   this  to  work.   The  input  parameters  are  left
  1091.               unchanged.   Takes pointers to two input sets, subtrahend
  1092.               which  is  subtracted from the minuend, and an output set
  1093.               that  will  contain  the set difference.  Returns SUCCESS
  1094.               if  the  difference  can be computed.  Otherwise, FAILURE
  1095.               is returned.  
  1096.  
  1097.            m) int set_member_count(set *aset); 
  1098.  
  1099.                    Returns the  number  of  members  in a set.  Takes a
  1100.               pointer to a set.  
  1101.  
  1102.            n) boolean set_equality(set *left, set *right); 
  1103.  
  1104.                    Two sets  are  equal  if they have the same type and
  1105.               size  and  the  identical  membership.  Takes pointers to
  1106.               two  input  sets  and  returns TRUE if the sets are equal
  1107.               and FALSE if they are not equal.  
  1108.  
  1109.            o) boolean set_inequality(set *left, set *right); 
  1110.  
  1111.                    Two sets  are  equal  if they have the same type and
  1112.               size  and  the  identical  membership.  Takes pointers to
  1113.               two  input  sets  and  returns  TRUE  if the sets are NOT
  1114.               equal and FALSE if the sets ARE equal.  
  1115.  
  1116.            p) boolean set_included_in(set *left, set *right); 
  1117.  
  1118.                    The left  set  is included in the right set if every
  1119.               member  of  left  is  a  member of right (left <= right).
  1120.               Takes  pointers  to two sets and returns TRUE if the left
  1121.               set  is  included  in  the  right  set and FALSE if it is
  1122.               not.  
  1123.  
  1124.            q) boolean set_includes(set *left, set *right); 
  1125.  
  1126.                    The right  set  is included in the left set if every
  1127.               member  of  right  is  a  member of left (left >= right).
  1128.               Takes  pointers to two input sets and returns TRUE if the
  1129.               left  set  includes  the  right  set and FALSE if it does
  1130.               not.  
  1131.  
  1132.            r) boolean cmp_base_types(set *seta,set *setb); 
  1133.  
  1134.                    Compares base_types  of two sets.  Takes pointers to
  1135.               two  sets  and  returns  TRUE  if  one  of  the  sets has
  1136.               UNIVERSAL  base_type  or  if  both  sets  have  the  same
  1137.               base_type.  FALSE is returned, otherwise.  
  1138.  
  1139.  
  1140.  
  1141.       
  1142.  
  1143.  
  1144.       Issue 16                     C News                            20
  1145.  
  1146.  
  1147.            s) boolean cmp_set_tags(set *seta,set *setb); 
  1148.  
  1149.                    Compares set_tags  of  two  different  sets.   Takes
  1150.               pointers  to  two sets and returns TRUE if both sets have
  1151.               the same set_tags.  
  1152.  
  1153.            t) int set_of(set *aset,...); 
  1154.  
  1155.                    This function  assigns  a  set  of  values  to a set
  1156.               variable  after  clearing  any contents of that variable.
  1157.               The  variable's  parameters  must  be able to accommodate
  1158.               the  set of values.  Takes a pointer to a set followed by
  1159.               set   constructor   expressions.    The  set  constructor
  1160.               expressions   are   followed  by  an  End-Of-List  symbol
  1161.               consisting  of  an  under-  score character followed by a
  1162.               capital  E,  or,  _E.  Constructor expressions are almost
  1163.               identical  with  those  used in Pascal with the exception
  1164.               that  subrange  limits  are  separated by two consecutive
  1165.               underscore  characters  (__) rather than two dots (..) as
  1166.               in   Pascal.    Typical   constructor  expressions  might
  1167.               include the following: 
  1168.  
  1169.                       defset(aset,CHARACTER,256,0);
  1170.                               .
  1171.                    .
  1172.                    .
  1173.                       set_of(&aset,'A'__'Z','a'__'z','\x01',92,_E);
  1174.  
  1175.               In  the  above example, a CHARACTER set is populated with
  1176.               upper   and  lower  case  alphabets;  character  1  (i.e.
  1177.               Control-A); and character 92, a backward slash.  
  1178.  
  1179.            u) int set_args(set *aset, va_list ap); 
  1180.  
  1181.                    Extracts all  the  set members from a parameter list
  1182.               passed  to the caller of this function.  This function is
  1183.               for internal use by the set_of function.  
  1184.  
  1185.  
  1186.  
  1187.       3. IMPLEMENTATION        
  1188.  
  1189.            The functions    and   definitions   discussed   above   are
  1190.       implemented  in  Turbo-C,  Version 2.0 and are contained in files
  1191.       SETS.C  and  SETS.H.   In  addition,  a test file, SET_TEST.C, is
  1192.       included  to  illustrate the use of "sets."  I tried hard to keep
  1193.       the  implementation  as close to ANSI C as I could.  However, the
  1194.       only  ANSI  C  compiler I have access to is the Turbo-C compiler.
  1195.       Perhaps  someone  with  some  other  ANSI compilers would run the
  1196.       test  program  and  include  the results in the same archive file
  1197.       just  to prove whether sets can be run someplace other than under
  1198.       Turbo-C.  
  1199.  
  1200.  
  1201.       
  1202.  
  1203.  
  1204.       Issue 16                     C News                            21
  1205.  
  1206.  
  1207.  
  1208.            Sets was  a  project that occupied me for about a solid week
  1209.       over  a  period  of  a few months.  I'm looking forward to having
  1210.       the  set  capability for my C programs and I suspect that my time
  1211.       investment  will  be  paid back quickly.  I hope you get some use
  1212.       out of it as well.  
  1213.  
  1214.  
  1215.       4. THE SETSX.LIBraries       
  1216.  
  1217.            Having made  the  sets  functions, I realized that each time
  1218.       they  were  used,  the  resulting  executable module increased in
  1219.       size  by about 2500 bytes.  I felt this was too large an increase
  1220.       despite  the  neat  new  capabilities so I decided to create some
  1221.       libraries  so  I  could  use  just the functions I needed thereby
  1222.       incurring  minimum  code size penalty.  Happily, Borland makes it
  1223.       relatively  easy  to  create  libraries  with their MAKE and TLIB
  1224.       utilities.   The  following is a "cookbook" to tell you how I did
  1225.       it and, hopefully, it isn't too ridiculous: 
  1226.  
  1227.            As I  said  above,  an  advantage  of  having  functions  in
  1228.       libraries  is  that you may use functions from a large collection
  1229.       without  having  to  link all your clever code into each program.
  1230.       This  is  achieved  in a very straightforward manner; by breaking
  1231.       the  code into use-sized pieces or single functions, compiling it
  1232.       as  individual  functions  to  form  individual  linkable  object
  1233.       modules,  and then collecting them in a single file or library so
  1234.       that  your  linker  can  extract  them individually as needed for
  1235.       your  programs.   Oh  yes!  One  important thing.  All the object
  1236.       modules  in a single library must be compiled for the same memory
  1237.       model.   If  you  work  with more than one memory model, you will
  1238.       need a "sets" library for each memory model you use.  
  1239.  
  1240.            Borland's MAKE  program is usually used to generate software
  1241.       using  compilers and linkers.  I used Borland's TCC and a library
  1242.       manager,  Borland's  TLIB.   I used several files to describe the
  1243.       end  result,  six library files, one for each of Borland's memory
  1244.       models.  The  MAKE  program  required  a  makefile which I called
  1245.       SETS.MAK.   The  makefile  referenced TLIB which, in turn, called
  1246.       upon  a  response  file  called SETS.RSP. Being a neophyte to the
  1247.       use  of  MAKE,  I  took the easy way out and used a batch file to
  1248.       call  MAKE six times and I passed model-determining parameters to
  1249.       MAKE  each time in order to create the six libraries. These files
  1250.       are provided with this article for your use.  
  1251.  
  1252.            When you  review  these  files, you will, no doubt, be a bit
  1253.       taken  aback  by  the  path notation used throughout.  This was a
  1254.       consequence  of  the subdirectory structure I happen to be using.
  1255.       It  was  necessary  to carefully indicate the directory structure
  1256.       to  MAKE,  etc.,  to  achieve  a  usable result.  My subdirectory
  1257.       structure is as follows: 
  1258.  
  1259.  
  1260.  
  1261.       
  1262.  
  1263.  
  1264.       Issue 16                     C News                            22
  1265.  
  1266.  
  1267.                                 Turbo-C
  1268.                               Subdirectory
  1269.                                  "TC"
  1270.                                    |
  1271.        ____________________________|______________    Object Module
  1272.        |                       |                 |     Subdirectories 
  1273.       TCC.EXE                "SETS"              |-------"OUTPUT.T"
  1274.       MAKE.EXE            Subdirectory           |-------"OUTPUT.S"
  1275.       TLIB.EXE                 |                 |-------"OUTPUT.M"
  1276.                                |                 |-------"OUTPUT.C"
  1277.                          C Source Code           |-------"OUTPUT.L"
  1278.                            for SETS              |-------"OUTPUT.H"
  1279.                          LIB files for
  1280.                              SETS
  1281.                          LST files for
  1282.                              SETS
  1283.  
  1284.       The   MAKE   program   runs   other  programs  according  to  the
  1285.       instructions  contained  in a text file.  The layout of this .MAK
  1286.       file is as follows: 
  1287.  
  1288.           column 1
  1289.       _______|
  1290.       |
  1291.       V
  1292.       # This is a comment.  It ends whatever line it's found on.
  1293.                           # It can begin in any column.
  1294.  
  1295.           <<< blank lines are permitted >>>
  1296.  
  1297.       # Macro for compiler directory locations...This defines the
  1298.       # compiler and the locations of all the required libraries,
  1299.       # include files, and a place to put the output .OBJ file.
  1300.       # Note that the destination for the compiler output is,
  1301.       # itself a macro expansion.  In this case, the macro, MDL,
  1302.       # is supplied on the MAKE command line in the file, MAKESETS.BAT.
  1303.  
  1304.       COMPILER = d:\tc\tcc -c -C -Ld:\tc\lib -Id:\tc\include \
  1305.               -nd:\tc\output.$(MDL)
  1306.  
  1307.       # The first "executable" line of the .MAK file defines the
  1308.       # product of the MAKE operation.  In this case, it's a library
  1309.       # file called SETS$(MDL).LIB.  Note the full path specification
  1310.       # qualifying all the filenames.  The backslash, \, is used as a
  1311.       # line continuation signal for MAKE.
  1312.  
  1313.       d:\tc\sets\SETS$(MDL).LIB: d:\tc\output.$(MDL)\DUMPSET.OBJ \
  1314.           d:\tc\output.$(MDL)\SETPRINT.OBJ \
  1315.                            .
  1316.                            .
  1317.                            .
  1318.           d:\tc\output.$(MDL)\SETOF.OBJ  # last OBJ file
  1319.  
  1320.  
  1321.       
  1322.  
  1323.  
  1324.       Issue 16                     C News                            23
  1325.  
  1326.  
  1327.  
  1328.       # The format is  PRODUCT: DEPENDENCIES
  1329.       #                     RULES
  1330.       # The colon after the product signals the beginning of the list
  1331.       # of files that the product is dependent upon.  If any of the 
  1332.       # times/dates of the dependencies are after those of the product,
  1333.       # the product will be re-created according to the rules.  Or, if
  1334.       # the files don't exist they are created. In this case, the rules 
  1335.       # are
  1336.  
  1337.          cd \tc\output.$(MDL)
  1338.          d:\tc\tlib d:\tc\sets\sets$(MDL).lib /E @d:\tc\sets\sets.rsp,\
  1339.           d:\tc\sets\sets$(MDL).lst
  1340.          cd \tc\sets
  1341.  
  1342.       # Product-specification/dependency entries begin in column 1 and
  1343.       # rules begin in columns other than 1 (i.e. after a tab or space).
  1344.       # In this case, the rules require a switch to a subdirectory
  1345.       # determined by the MDL macro followed by operation of the TLIB
  1346.       # program which combines all the object files defined in a
  1347.       # response file (consisting of all the modules in the library)
  1348.       # and a switch back to the original directory.
  1349.  
  1350.       # The dependency concept is carried a step further, however, by
  1351.       # adding additional "executable" lines to the .MAK file.  The
  1352.       # same format is used as for the main product as shown below:
  1353.  
  1354.       d:\tc\output.$(MDL)\DUMPSET.OBJ:  d:\tc\sets\dumpset.c \
  1355.           d:\tc\sets\sets.h
  1356.         $(COMPILER) -m$(MDL) d:\tc\sets\DUMPSET.C 
  1357.  
  1358.       # In this case, one of the .OBJ files is the product and it's
  1359.       # dependent upon its C source code as well as a special
  1360.       # purpose header file.  If the date/time of either of these files
  1361.       # is later than that of the .OBJ file, the rule(s) following the 
  1362.       # product line will be executed.  Note that MAKE follows the chain
  1363.       # of dependencies down to its end before actually executing the
  1364.       # rules.  Note that in the above executable lines for DUMPSET.OBJ,
  1365.       # the COMPILER macro is used to direct the compiler to the proper
  1366.       # subdirectories.  The Turbo C Reference Guide, Appendix D, 
  1367.       # contains instructions and more features for the MAKE program.
  1368.       # Instructions for TLIB are contained in the same place.
  1369.  
  1370.  
  1371.       5. USING THE LIBRARIES      
  1372.  
  1373.            With Turbo  C's  integrated environment, it's really easy to
  1374.       use  the  SETS  libraries.  Use a project file (.PRJ file) and at
  1375.       the  end  of  the .PRJ file, add a line with the name of the .LIB
  1376.       file  you  want  to  use.  The following is the .PRJ file used to
  1377.       make  the  SET_TEST.EXE  program included with this article using
  1378.       Turbo C's "huge" memory model.  
  1379.  
  1380.  
  1381.       
  1382.  
  1383.  
  1384.       Issue 16                     C News                            24
  1385.  
  1386.  
  1387.  
  1388.  
  1389.            set_test.c 
  1390.  
  1391.            setsh.lib 
  1392.  
  1393.       Simple   enough.   The  first  line  tells  Turbo  C  to  compile
  1394.       SET_TEST.C   and  the  last  line  tells  the  linker  to  search
  1395.       SETSH.LIB  for referenced but not yet found object modules before
  1396.       searching  the standard, CH.LIB.  Press the MAKE function key and
  1397.       it's done.  
  1398.  
  1399.            With the  command line compiler, TCC, just use the same list
  1400.       of files as you would in the .PRJ file as follows: 
  1401.  
  1402.  
  1403.            tcc -mh -Id:\tc\include -Ld:\tc\lib set_test setsh.lib 
  1404.  
  1405.            In both  cases,  the  linker  will  look  for  all  the SETS
  1406.       libraries  located  in the d:\tc\lib subdirectory unless you have
  1407.       indicated that .LIB files may be found elsewhere.  
  1408.  
  1409.            If you  have  used  the  correct  invocations  of  the  SETS
  1410.       functions  as  described  above,  you  will correctly link in the
  1411.       needed functions and you've got sets capability in C.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.       
  1442.  
  1443.  
  1444.       Issue 16                     C News                            25
  1445.  
  1446.  
  1447.       ================================================================= 
  1448.       ARTICLE SUBMISSION STANDARDS      
  1449.       ================================================================= 
  1450.  
  1451.  
  1452.  
  1453.  
  1454.            All articles,  reviews  and  letters  to  editor  should  be
  1455.       submitted  in  a  ASCII formatted file.  Please use 0-65 margins,
  1456.       and  single  space.   Do  not format the text in anyway, as I use
  1457.       Proff  to  format C News.  Proff takes care of the justification,
  1458.       footnotes and headers.   
  1459.  
  1460.  
  1461.            You can  send in your article on a diskette to the C BBS, or
  1462.       upload  it  to  the C BBS. See "How to Contact us here at C News"
  1463.       for more information.   
  1464.  
  1465.       Barry 
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.       
  1502.  
  1503.  
  1504.       Issue 16                     C News                            26
  1505.  
  1506.  
  1507.       ================================================================= 
  1508.       HOW TO GET HOLD OF US HERE AT CNEWS
  1509.       ================================================================= 
  1510.  
  1511.  
  1512.  
  1513.  
  1514.            The primary address for C News is as follows: 
  1515.  
  1516.  
  1517.            C News 
  1518.  
  1519.            % BCL Limited 
  1520.  
  1521.            P.O. Box 9162 
  1522.  
  1523.            McLean, VA 22102 
  1524.  
  1525.            USA 
  1526.  
  1527.  
  1528.            The primary electronic address for C News is the C BBS: 
  1529.  
  1530.  
  1531.            C BBS 
  1532.  
  1533.            1-703-644-6478 
  1534.  
  1535.            2400,8,N,1 23hrs a day.  
  1536.  
  1537.            1:109/307 in Fidonet.  
  1538.  
  1539.  
  1540.            The secondary electronic address for C News is: 
  1541.  
  1542.  
  1543.            MCI Mail: BCL Limited 
  1544.  
  1545.  
  1546.            Barry 
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.       
  1562.  
  1563.